package com.xzj.common.id.gen;

import com.xzj.common.id.contract.IIdGenerator;
import com.xzj.common.id.domain.IdGeneratorOptions;
import com.xzj.common.id.exception.IdGeneratorException;

/**
 * @program: xzj-common
 * @ClassName: IdHelper
 * @description:
 * @author: XiaZunJun
 * @create: 2024-07-29 14:14
 */
public class IdHelper {
    private static IIdGenerator idGenInstance = null;

    public static IIdGenerator getIdGenInstance() {
        return idGenInstance;
    }

    /**
     * 设置参数，建议程序初始化时执行一次
     */
    public static void setIdGenerator(IdGeneratorOptions options) throws IdGeneratorException {
        idGenInstance = new DefaultIdGenerator(options);
    }

    /**
     * 生成新的Id
     * 调用本方法前，请确保调用了 SetIdGenerator 方法做初始化。
     *
     * @return
     */
    public static long nextId() throws IdGeneratorException {
        // if (idGenInstance == null) {
        // idGenInstance = new DefaultIdGenerator(new IdGeneratorOptions((short) 1));
        // }

        if (idGenInstance == null)
            throw new IdGeneratorException("Please initialize Yitter.IdGeneratorOptions first.");

        return idGenInstance.newLong();
    }

    public static String nextIdStr() throws IdGeneratorException {
        return String.valueOf(nextId());
    }
}
